package com.lee.mao.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class BaseActivity extends Activity {

    private static final String TAG = "BaseActivity";
    //protected Mapplication mapplication;

    protected Context ctx;

    protected SharedPreferences sp;

    public static void setFullScreen(Activity activity) {
        int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE;
        activity.getWindow().getDecorView().setSystemUiVisibility(flags);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//        setRequestedOrie ntation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
//        setFullScreen(this);//设置全屏
        sp = getSharedPreferences("mao", Context.MODE_PRIVATE);
        ctx = this;
    }

    protected ProgressDialog showDialog;


    public void showLoading(String msg) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                try {
                    hideLoading();
                    showDialog = ProgressDialog.show(BaseActivity.this, "", msg);
                    showDialog.setCanceledOnTouchOutside(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }


    public void hideLoading() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                try {
                    if (showDialog != null && showDialog.isShowing())
                        showDialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }


    /**
     * startActivity
     * 跳转到下一个页面
     *
     * @param clazz
     */
    public void nextActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * startActivity then finish
     * 跳转到下一个页面然后销毁当前页面
     */
    public void nextActivityThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }


    public void showToash(final String msg) {

        if (Thread.currentThread() == Looper.getMainLooper().getThread())
            // 如果在主线程中
            show(msg);
        else
            // 在子线程中
            runOnUiThread(new Runnable() {
                public void run() {
                    show(msg);
                }
            });
    }

    private void show(final String text) {

        try {
            if (!TextUtils.isEmpty(text))
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            //Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


} 